Code
import sys
print(sys.version)3.11.4 (v3.11.4:d2340ef257, Jun 6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)]
Tony Duan
October 1, 2023

https://www.python.org/downloads/macos/
python: /Library/Frameworks/Python.framework/Versions/3.11/bin/python3
libpython: /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/config-3.11-darwin/libpython3.11.dylib
pythonhome: /Library/Frameworks/Python.framework/Versions/3.11:/Library/Frameworks/Python.framework/Versions/3.11
version: 3.11.4 (v3.11.4:d2340ef257, Jun 6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)]
numpy: /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy
numpy_version: 1.26.0
NOTE: Python version was forced by RETICULATE_PYTHON
it may using version 3.9.18 under miniconda
but in terminal,system is using python 3.11.4
Then we find the python path in terminal
create .Renviron file containing the line RETICULATE_PYTHON=“xxxx/python3” in home directory
put RETICULATE_PYTHON=“/Library/Frameworks/Python.framework/Versions/3.11/bin/python3”
What is pip? pip — or pip3, as it’s also known — is a package management system for Python that is used to install and manage software packages. It’s the tool recommended by the Python Software Foundation for installing Python applications. It connects to an online repository of public packages called Python Package Index. pip is installed by default with Python 3.4 and later.if not then run in terminal
install jupyter in terminal
python3 -m pip install opendatasets
install pandas in r
check pandas version
list all installed pacakge in terminal
update numpy
uninstall numpy
open jupyter
check jupyter
open jupyter lab
exit edit mode
enter edit mode
Change cell to code
Change cell to markdown
Insert cell above
Insert cell below
delete cell
Undo last action
Redo last action
https://noteable.io/blog/jupyter-notebook-shortcuts-boost-productivity/
https://stackoverflow.com/questions/50145643/unable-to-change-python-path-in-reticulate/58743111#58743111
---
title: "Python set up on Mac"
author: "Tony Duan"
date: "2023-10-01"
categories: [Python]
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: left
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
---
{width="518"}
## download python
https://www.python.org/downloads/macos/
## check installed python version
### in python code
```{python}
import sys
print(sys.version)
```
### in r code
```{r}
reticulate::py_config()
```
*it may using version 3.9.18 under miniconda
### in terminal
```{python}
#| eval: false
jinchaoduan@Jinchaos-MacBook-Air ~ % python3
Python 3.11.4 (v3.11.4:d2340ef257, Jun 6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
```
```{python}
#| eval: false
jinchaoduan@Jinchaos-MacBook-Air ~ % which python3
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3
>>>
```
*If Python version is differnt on Rstuido and terminal then we find the python path in terminal
## config which python we use in rstudio
create .Renviron file containing the line RETICULATE_PYTHON="xxxx/python3" in home directory
```{python}
#| eval: false
touch $HOME/.Renviron
```
```{python}
#| eval: false
open $HOME/.Renviron
```
put RETICULATE_PYTHON="/Library/Frameworks/Python.framework/Versions/3.11/bin/python3"
### in mac terminal
```{python}
#| eval: false
python3 --version
```
## install pip
What is pip? pip --- or pip3, as it's also known --- is a package management system for Python that is used to install and manage software packages. It's the tool recommended by the Python Software Foundation for installing Python applications. It connects to an online repository of public packages called Python Package Index. pip is installed by default with Python 3.4 and later.if not then run in terminal
```{python}
#| eval: false
python -m ensurepip --upgrade
```
## install python package
### install python package in mac terminal
install jupyter in terminal
```{python}
#| eval: false
python3 -m pip install seaborn
```
python3 -m pip install opendatasets
### install python package in r code
install pandas in r
```{python}
#| eval: false
library(reticulate)
py_install("pandas")
```
## check python package version
check pandas version
```{python}
import pandas
print(pandas.__version__)
```
list all installed pacakge in terminal
```{python}
#| eval: false
pip list
```
## update python package
### in terminal
update numpy
```{python}
#| eval: false
python3 -m pip install --upgrade numpy
```
## uninstall package
uninstall numpy
```{python}
#| eval: false
python3 -m pip uninstall numpy
```
## install jupyter on mac
```{python}
#| eval: false
pip3 install jupyter
```
open jupyter
```{python}
#| eval: false
jupyter notebook
```
check jupyter
```{python}
#| eval: false
which jupyter
```
## install jupyter lab on mac
```{python}
#| eval: false
pip3 install jupyterlab
```
open jupyter lab
```{python}
#| eval: false
jupyter lab
```
## jupyter notebook short cut
exit edit mode
```{python}
#| eval: false
esc
```
enter edit mode
```{python}
#| eval: false
enter
```
Change cell to code
```{python}
#| eval: false
y
```
Change cell to markdown
```{python}
#| eval: false
m
```
Insert cell above
```{python}
#| eval: false
a
```
Insert cell below
```{python}
#| eval: false
b
```
delete cell
```{python}
#| eval: false
d+d
```
Undo last action
```{python}
#| eval: false
Command + Z
```
Redo last action
```{python}
#| eval: false
Command + Y
```
## get current working directory
```{python}
import os
# get the current working directory
current_working_directory = os.getcwd()
print(current_working_directory)
```
## change working directory
```{python}
import os
# change directory
os.chdir('/Users/jinchaoduan/Documents/GitHub')
print(os.getcwd())
```
## read csv
```{python}
import sys
import pandas as pd
```
```{python}
df = pd.read_csv('MrBeast_youtube_stats.csv')
```
```{python}
df.head()
```
## Reference
https://noteable.io/blog/jupyter-notebook-shortcuts-boost-productivity/
https://stackoverflow.com/questions/50145643/unable-to-change-python-path-in-reticulate/58743111#58743111